package com.hx.swing;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class FileCopy extends JFrame implements ActionListener {
    private JButton button1;
    private JButton button2;
    private JTextField textField1;
    private JTextField textField2;
    private JButton cpyBtn;

    public FileCopy() {
        setTitle("文件复制");
        setSize(600, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(null);//空布局，需要设置x，y

        JLabel label = new JLabel("源文件");
        textField1 = new JTextField();
        button1 = new JButton("...");
        label.setBounds(100, 50, 50, 25);
        add(label);
        textField1.setBounds(170, 50, 300, 25);
        add(textField1);
        button1.setBounds(480, 50, 50, 25);
        add(button1);
        button1.addActionListener(this);

        JLabel labe2 = new JLabel("目的地");
        textField2 = new JTextField();
        button2 = new JButton("...");
        labe2.setBounds(100, 100, 50, 25);
        add(labe2);
        textField2.setBounds(170, 100, 300, 25);
        add(textField2);
        button2.setBounds(480, 100, 50, 25);
        add(button2);
        button2.addActionListener(this);

        cpyBtn = new JButton("复制");
        cpyBtn.setBounds(200, 150, 200, 25);
        add(cpyBtn);
        cpyBtn.addActionListener(this);

        JProgressBar progressBar = new JProgressBar();
        progressBar.setBounds(100, 250, 450, 25);
        add(progressBar);
        progressBar.setStringPainted(true);
        progressBar.setValue(0);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FileCopy();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        if (o == button1) {
            JFileChooser fileChooser = new JFileChooser();
            int f = fileChooser.showOpenDialog(this);
            if (f == JFileChooser.APPROVE_OPTION) {
                textField1.setText(fileChooser.getSelectedFile().getPath());
            }
        } else if (o == button2) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int f = fileChooser.showOpenDialog(this);
            if (f == JFileChooser.APPROVE_OPTION) {
                textField2.setText(fileChooser.getSelectedFile().getPath());
            }
        }else if (o==cpyBtn){
            copy(button1.getText(),button2.getText());

        }
    }
    public void copy(String souce,String kk){
        try {
            InputStreamReader ss=new InputStreamReader(new FileInputStream(souce));
            OutputStreamWriter sw =new OutputStreamWriter(new FileOutputStream(kk));/*如果编码不同则需要
相应的转换一下才可以使用*/
            int len;
            char[] wigth=new char[1598];

            while ((len=ss.read(wigth))!=-1){
                sw.write(wigth,0,len);
                System.out.print((char)len);
            }
    } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }}